import inquirer from 'inquirer'
import ci from 'miniprogram-ci'

const KYES = {
  question1: '已打包成功，请选择执行操作',
  question2: '请输入上传描述',
}
const PROJECT_CONFIG = {
  es6: false, // 设置true编译会报错
  minify: true,
  codeProtect: true,
}

;(async () => {
  const project = new ci.Project({
    appid: 'wxa901702c983ad733', // appid
    type: 'miniProgram', // 固定小程序类型
    projectPath: './dist/build/mp-weixin/',
    privateKeyPath: './upload/private.wxa901702c983ad733.key',
    ignores: [],
  })
  const res = await inquirer.prompt([
    {
      type: 'list',
      name: KYES.question1,
      choices: ['跳过', '预览', '上传'],
    },
  ])
  if (res[KYES.question1] === '跳过') return
  if (res[KYES.question1] === '预览') {
    await ci.preview({
      project,
      desc: '预览', // 此备注将显示在“小程序助手”开发版列表中
      setting: Object.assign({}, PROJECT_CONFIG),
      qrcodeFormat: 'image',
      qrcodeOutputDest: './upload/qrcode.jpg',
      onProgressUpdate: console.log,
      // pagePath: 'pages/index/index', // 预览页面
      // searchQuery: 'a=1&b=2', // 预览参数 [注意!]这里的`&`字符在命令行中应写成转义字符`\&`
    })
    return
  }
  const input = await inquirer.prompt([
    {
      type: 'input',
      name: KYES.question2,
      default: '版本更新',
    },
  ])
  await ci.upload({
    project,
    version: '1.1.1',
    desc: input[KYES.question2],
    robot: 1, // 指定机器人版本号
    setting: Object.assign({}, PROJECT_CONFIG),
    onProgressUpdate: console.log,
  })
})()
